cap program drop table_prog
program define table_prog
syntax, rhsvars(string) filename(string) [condition(string)]
	eststo clear

	eststo: areg voted_rescaled `rhsvars' `condition', absorb(election_year_desc) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "No", replace

	eststo: areg voted_rescaled `rhsvars' i.election_year_desc `condition', absorb(customer_code) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "Yes", replace

	eststo: areg ran_rescaled `rhsvars' `condition', absorb(election_year_desc) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "No", replace

	eststo: areg ran_rescaled `rhsvars' i.election_year_desc `condition', absorb(customer_code) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "Yes", replace

	eststo: areg winner_rescaled `rhsvars' `condition', absorb(election_year_desc) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "No", replace

	eststo: areg winner_rescaled `rhsvars' i.election_year_desc `condition', absorb(customer_code) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "Yes", replace

	esttab using "`filename'.tex", se label drop(*election_year*) stats(ymean yfe ffe r2, label("Mean of outcome (\%)" "Year FE" "Farmer FE" "\$R^{2}\$")) nostar mgroups("Voted" "Ran" "Won", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles(none) collabels(none) mlabels(none) compress booktabs frag float width(\hsize) replace
end


cap program drop table_prog_strat
program define table_prog_strat
syntax, dv(string) rhsvars(string) filename(string) [condition(string)]
	* get 25, 50, 75 percentiles of avg payment
	preserve
		collapse (mean) amt_all, by(customer_code) fast
		su amt_all if amt_all > 0, d
		loc p25 = r(p25)
		loc p50 = r(p50)
		loc p75 = r(p75)
	restore

	* regressions
	eststo clear

	eststo: areg `dv' `rhsvars' i.election_year_desc if avg_amt_all <= `p25', absorb(customer_code) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "Yes", replace
	loc nobs_f : di %12.0fc e(N)
	estadd loc nobs =  "`nobs_f'"
	loc nclust_f : di %12.0fc e(N_clust)
	estadd loc nclust = "`nclust_f'"
	
	eststo: areg `dv' `rhsvars' i.election_year_desc if avg_amt_all <= `p50' & avg_amt_all > `p25', absorb(customer_code) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "Yes", replace
	loc nobs_f : di %12.0fc e(N)
	estadd loc nobs =  "`nobs_f'"
	loc nclust_f : di %12.0fc e(N_clust)
	estadd loc nclust = "`nclust_f'"
	
	eststo: areg `dv' `rhsvars' i.election_year_desc if avg_amt_all <= `p75' & avg_amt_all > `p50', absorb(customer_code) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "Yes", replace
	loc nobs_f : di %12.0fc e(N)
	estadd loc nobs =  "`nobs_f'"
	loc nclust_f : di %12.0fc e(N_clust)
	estadd loc nclust = "`nclust_f'"
	
	eststo: areg `dv' `rhsvars' i.election_year_desc if avg_amt_all > `p75', absorb(customer_code) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "Yes", replace
	loc nobs_f : di %12.0fc e(N)
	estadd loc nobs =  "`nobs_f'"
	loc nclust_f : di %12.0fc e(N_clust)
	estadd loc nclust = "`nclust_f'"
	
	esttab using "`filename'.tex", se label drop(*election_year*) stats(ymean yfe ffe r2 nclust nobs, label("Mean of outcome (\%)" "Year FE" "Farmer FE" "\$R^{2}\$" "Num. Clusters" "Observations")) nostar mgroups("Below 25th pctile." "Btwn. 25th and 50th pctile." "Btwn 50th and 75th pctile." "Above 75th pctile.", pattern(1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles(none) collabels(none) mlabels(none) compress booktabs frag float width(\hsize) replace
end


cap program drop table_prog_xtlogit
program define table_prog_xtlogit
syntax, rhsvars(string) filename(string) [condition(string)]
	preserve
	egen customer_code_num = group(customer_code)
	xtset customer_code_num election_year_desc
	
	eststo clear

	eststo logit1: xtlogit voted `rhsvars' i.election_year_desc `condition'
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "Yes", replace
	eststo margin1: margins, dydx(`rhsvars') post
	
	eststo logit2: xtlogit ran `rhsvars' i.election_year_desc `condition'
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "Yes", replace
	eststo margin2: margins, dydx(`rhsvars') post
	
	eststo logit3: xtlogit winner `rhsvars' i.election_year_desc `condition'
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "Yes", replace
	eststo margin3: margins, dydx(`rhsvars') post
	restore

	esttab logit1 margin1 logit2 margin2 logit3 margin3 using "`filename'.tex", eqlabels(none) se label drop(*election_year*) stats(ymean yfe ffe, label("Mean of outcome (\%)" "Year FE" "Farmer FE")) nostar mgroups("Voted" "Ran" "Won", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitle("" "Margins" "" "Margins" "" "Margins") compress booktabs frag float width(\hsize) replace
end



cap program drop table_prog_margins
program define table_prog_margins
syntax, amt_var(string) size_var(string) filename(string) [condition(string)]
	// get percentiles of size of avg county size across years
	preserve
		bys hq_county_code election_year_desc: keep if _n == 1
		collapse (mean) `size_var', by(hq_county_code) fast
		qui su `size_var', d
		loc p25 = r(p25)
		loc p50 = r(p50)
		loc p75 = r(p75)
	restore
	
	
	// get max and min for amt var for plotting
	qui su `amt_var', d
	loc amt_var_min = r(min)
	loc amt_var_max = r(max)
	
	
	// get labels for plots
	local xlab: variable label `amt_var'
	
	
	// estimation
	eststo clear

	* voted
	eststo: areg voted_rescaled c.`amt_var'##c.`size_var' `condition', absorb(election_year_desc) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "No", replace
	eststo: margins, dydx(`amt_var') at(`size_var'=(`p25' `p50' `p75')) vsquish post
	
	qui areg voted_rescaled c.`amt_var'##c.`size_var' `condition', absorb(election_year_desc) cluster(customer_code)
	qui margins, at(`amt_var'=(`amt_var_min' `amt_var_max') `size_var'=(`p25' `p50' `p75')) vsquish post
	marginsplot, noci x(`amt_var') ytitle("Voted x 100 (predicted)") xtitle(`xlab') title("Predictive Margins") recast(line)
	graph export "`filename'_voted1.pdf", replace

	
	eststo: areg voted_rescaled c.`amt_var'##c.`size_var' i.election_year_desc `condition', absorb(customer_code) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "Yes", replace
	eststo: margins, dydx(`amt_var') at(`size_var'=(`p25' `p50' `p75')) vsquish post
	
	qui areg voted_rescaled c.`amt_var'##c.`size_var' i.election_year_desc `condition', absorb(customer_code) cluster(customer_code)
	qui margins, at(`amt_var'=(`amt_var_min' `amt_var_max') `size_var'=(`p25' `p50' `p75')) vsquish post
	marginsplot, noci x(`amt_var') ytitle("Voted x 100 (predicted)") xtitle(`xlab') title("Predictive Margins") recast(line)
	graph export "`filename'_voted2.pdf", replace


	* ran
    eststo: areg ran_rescaled c.`amt_var'##c.`size_var' `condition', absorb(election_year_desc) cluster(customer_code)
    estadd ysumm, mean
    estadd loc yfe "Yes", replace
    estadd loc ffe "No", replace
    eststo: margins, dydx(`amt_var') at(`size_var'=(`p25' `p50' `p75')) vsquish post
    
    qui areg ran_rescaled c.`amt_var'##c.`size_var' `condition', absorb(election_year_desc) cluster(customer_code)
    qui margins, at(`amt_var'=(`amt_var_min' `amt_var_max') `size_var'=(`p25' `p50' `p75')) vsquish post
	marginsplot, noci x(`amt_var') ytitle("Ran x 100 (predicted)") xtitle(`xlab') title("Predictive Margins") recast(line)
    graph export "`filename'_ran1.pdf", replace

    
    eststo: areg ran_rescaled c.`amt_var'##c.`size_var' i.election_year_desc `condition', absorb(customer_code) cluster(customer_code)
    estadd ysumm, mean
    estadd loc yfe "Yes", replace
    estadd loc ffe "Yes", replace
    eststo: margins, dydx(`amt_var') at(`size_var'=(`p25' `p50' `p75')) vsquish post
    
    qui areg ran_rescaled c.`amt_var'##c.`size_var' i.election_year_desc `condition', absorb(customer_code) cluster(customer_code)
    qui margins, at(`amt_var'=(`amt_var_min' `amt_var_max') `size_var'=(`p25' `p50' `p75')) vsquish post
	marginsplot, noci x(`amt_var') ytitle("Ran x 100 (predicted)") xtitle(`xlab') title("Predictive Margins") recast(line)
    graph export "`filename'_ran2.pdf", replace
	
	
	* winner
    eststo: areg winner_rescaled c.`amt_var'##c.`size_var' `condition', absorb(election_year_desc) cluster(customer_code)
    estadd ysumm, mean
    estadd loc yfe "Yes", replace
    estadd loc ffe "No", replace
    eststo: margins, dydx(`amt_var') at(`size_var'=(`p25' `p50' `p75')) vsquish post
    
    qui areg winner_rescaled c.`amt_var'##c.`size_var' `condition', absorb(election_year_desc) cluster(customer_code)
    qui margins, at(`amt_var'=(`amt_var_min' `amt_var_max') `size_var'=(`p25' `p50' `p75')) vsquish post
	marginsplot, noci x(`amt_var') ytitle("Won x 100 (predicted)") xtitle(`xlab') title("Predictive Margins") recast(line)
    graph export "`filename'_winner1.pdf", replace

    
    eststo: areg winner_rescaled c.`amt_var'##c.`size_var' i.election_year_desc `condition', absorb(customer_code) cluster(customer_code)
    estadd ysumm, mean
    estadd loc yfe "Yes", replace
    estadd loc ffe "Yes", replace
    eststo: margins, dydx(`amt_var') at(`size_var'=(`p25' `p50' `p75')) vsquish post
    
    qui areg winner_rescaled c.`amt_var'##c.`size_var' i.election_year_desc `condition', absorb(customer_code) cluster(customer_code)
    qui margins, at(`amt_var'=(`amt_var_min' `amt_var_max') `size_var'=(`p25' `p50' `p75')) vsquish post
	marginsplot, noci x(`amt_var') ytitle("Won x 100 (predicted)") xtitle(`xlab') title("Predictive Margins") recast(line)
    graph export "`filename'_winner2.pdf", replace
	
	esttab using "`filename'.tex", eqlabels(none) drop(*election_year*) se label interaction(" x ") varlabels(1._at "Avg county pop., 25th pctile. (`p25')" 2._at "Avg county pop., 50th pctile. (`p50')" 3._at "Avg county pop., 75th pctile. (`p75')") stats(ymean yfe ffe r2, label("Mean of outcome (\%)" "Year FE" "Farmer FE" "\$R^{2}\$")) nostar mgroups("Voted" "Ran" "Won", pattern(1 0 0 0 1 0 0 0 1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles("" "Margins" "" "Margins" "" "Margins" "" "Margins" "" "Margins" "" "Margins") compress booktabs frag float width(\hsize) replace
end



cap program drop table_prog_poly
program define table_prog_poly
syntax, rhsvar(string) polynum(string) filename(string) [condition(string)]
	// get percentiles of payment for margins
	qui su `rhsvar' if `rhsvar' > 0, d
	loc p10 = r(p10)
	loc p25 = r(p25)
	loc p50 = r(p50)
	loc p75 = r(p75)
	loc p90 = r(p90)

	// get the right hand side string (build up interaction term)
	local rhsstr c.`rhsvar'
	loc polynum = `polynum' - 1
	if `polynum' > 0 {
		forvalues i = 1/`polynum' {
			local rhsstr `rhsstr'##c.`rhsvar'
		}
	}
	loc polynum = `polynum' + 1
	
	/*
	// gen variables if needed
	local rhslab: variable label `rhsvar'
	
	forvalues i = 2/`polynum' {
		cap gen `rhsvar'_`i' = `rhsvar'^`i'
		cap label var `rhsvar'_`i' "`rhslab' (order `i')"
	}
	
	// get the string to put in the command
	loc rhsstr `rhsvar'
	forvalues i = 2/`polynum' {
		local rhsstr `rhsstr' `rhsvar'_`i'
	}
	*/
	
	// regressions and plots
	eststo clear

	eststo: areg voted_rescaled `rhsstr' `condition', absorb(election_year_desc) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "No", replace
	margins, at(`rhsvar' = (`p10' `p25' `p50' `p75' `p90'))
	marginsplot, ytitle("Voted x 100 (predicted)") title("Predictive Margins (order `polynum')") scheme(s1mono)
	graph export "`filename'_poly`polynum'_voted1.pdf", replace

	eststo: areg voted_rescaled `rhsstr' i.election_year_desc `condition', absorb(customer_code) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "Yes", replace
	margins, at(`rhsvar' = (`p10' `p25' `p50' `p75' `p90'))
	marginsplot, ytitle("Voted x 100 (predicted)") title("Predictive Margins (order `polynum')") scheme(s1mono)
	graph export "`filename'_poly`polynum'_voted2.pdf", replace

	eststo: areg ran_rescaled `rhsstr' `condition', absorb(election_year_desc) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "No", replace
	margins, at(`rhsvar' = (`p10' `p25' `p50' `p75' `p90'))
	marginsplot, ytitle("Ran x 100 (predicted)") title("Predictive Margins (order `polynum')") scheme(s1mono)
	graph export "`filename'_poly`polynum'_ran1.pdf", replace

	eststo: areg ran_rescaled `rhsstr' i.election_year_desc `condition', absorb(customer_code) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "Yes", replace
	margins, at(`rhsvar' = (`p10' `p25' `p50' `p75' `p90'))
	marginsplot, ytitle("Ran x 100 (predicted)") title("Predictive Margins (order `polynum')") scheme(s1mono)
	graph export "`filename'_poly`polynum'_ran2.pdf", replace

	eststo: areg winner_rescaled `rhsstr' `condition', absorb(election_year_desc) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "No", replace
	margins, at(`rhsvar' = (`p10' `p25' `p50' `p75' `p90'))
	marginsplot, ytitle("Won x 100 (predicted)") title("Predictive Margins (order `polynum')") scheme(s1mono)
	graph export "`filename'_poly`polynum'_won1.pdf", replace

	eststo: areg winner_rescaled `rhsstr' i.election_year_desc `condition', absorb(customer_code) cluster(customer_code)
	estadd ysumm, mean
	estadd loc yfe "Yes", replace
	estadd loc ffe "Yes", replace
	margins, at(`rhsvar' = (`p10' `p25' `p50' `p75' `p90'))
	marginsplot, ytitle("Won x 100 (predicted)") title("Predictive Margins (order `polynum')") scheme(s1mono)
	graph export "`filename'_poly`polynum'_won2.pdf", replace

	esttab using "`filename'_poly`polynum'.tex", se coeflabels(`rhsvar' Amount c.`rhsvar'##c.`rhsvar' Amount$^2$ c.`rhsvar'##c.`rhsvar'##c.`rhsvar' Amount$^3$ c.`rhsvar'##c.`rhsvar'##c.`rhsvar'##c.`rhsvar' Amount$^4$ c.`rhsvar'##c.`rhsvar'##c.`rhsvar'##c.`rhsvar'##c.`rhsvar' Amount$^5$) drop(*election_year*) stats(ymean yfe ffe r2, label("Mean of outcome (\%)" "Year FE" "Farmer FE" "\$R^{2}\$")) nostar mgroups("Voted" "Ran" "Won", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles(none) collabels(none) mlabels(none) compress booktabs frag float width(\hsize) replace
end
